﻿using CPPEI.Coolzon.UI;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Designtime
{
    public static class ResPoolFactory
    {
        private static Dictionary<Type, IImageResPool> s_converterDictionary;

        private static UnityContainer _resPoolContainer;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="configFilePath"></param>
        public static void Init(string configFilePath)
        {
            if (_resPoolContainer == null)
            {
                _resPoolContainer = new UnityContainer();

                ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
                fileMap.ExeConfigFilename = configFilePath;

                System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
                UnityConfigurationSection section = (UnityConfigurationSection)config.GetSection("XResPool");

                section.Configure(_resPoolContainer, "Default");
            }
            else
            {
                throw new Exception("has inited");
            }
        }

        /// <summary>
        /// 获得图片资源池
        /// </summary>
        /// <returns></returns>
        public static IImageResPool GetImageResPool()
        {
            try
            {
                return _resPoolContainer.Resolve<IImageResPool>("ImageResPool");
            }
            catch (Exception ex)
            {
                Common.Logger.ErrorToTag(Library.LogTAG, "ResPoolFactory.GetImageResPool失败,ex:{0}", ex);
            }
            return null;
        }

        public static IDataSourcePool GetDataSourcePool()
        {
            try
            {
                return _resPoolContainer.Resolve<IDataSourcePool>("DataSourcePool");
            }
            catch (Exception ex)
            {
                Common.Logger.ErrorToTag(Library.LogTAG, "ResPoolFactory.GetDataSourcePool失败,ex:{0}", ex);
            }
            return null;
        }

        public static ITemplatePoll GetTemplatePool()
        {
            try
            {
                return _resPoolContainer.Resolve<ITemplatePoll>("TemplatePool");
            }
            catch (Exception ex)
            {
                Common.Logger.ErrorToTag(Library.LogTAG, "ResPoolFactory.GetTemplatePool,ex:{0}", ex);
            }
            return null;
        } 
    }
}
